package com.aa.controller;

import com.aa.entity.Orders;
import com.aa.entity.Users;
import com.aa.service.impl.GoodstypeServiceImpl;
import com.aa.service.impl.OrdersServiceImpl;
import com.aa.service.impl.UsersServiceImpl;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/admin")
public class OrdersCtrl {@Resource
OrdersServiceImpl ordersService;

    @Resource
    GoodstypeServiceImpl goodstypeService;
    @ApiOperation("获取所有商品")
    @RequestMapping(value = "/orders",method = {RequestMethod.POST,RequestMethod.GET})
    public String findAllGoods(Model model,
                               @RequestParam(value = "pageNum",required = false, defaultValue = "1") Integer pageNum
            , @RequestParam(value = "pageSize",required = false, defaultValue = "1") Integer pageSize){

        PageInfo<Orders> ordersList =  ordersService.queryAll( pageNum,pageSize);
        model.addAttribute("ordersListPage",ordersList);
        return "ordersList";
    }
    @ApiOperation("根据ID删除商品")
    @GetMapping("/delOrders/{id}")
    public String delOrders(@PathVariable("id") int id, Model model){
        ordersService.deleteByID(id);
        model.addAttribute("msg","删除成功");
        return "forward:/admin/orders";
    }
    // 通过request对象实现转发
//    @GetMapping("/delGoods/{id}")
//    public void delGoods(@PathVariable("id") int id, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        int num = goodsService.deleteById(id);
//        request.setAttribute("msg","删除成功！");
//        request.getRequestDispatcher("/goods").forward(request,response);
//    }

    // 通过ModelAndView对象实现转发
//    @GetMapping("/delGoods/{id}")
//    public ModelAndView delGoods(@PathVariable("id") int id) {
//        int num = goodsService.deleteById(id);
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("msg","删除成功！");
//        modelAndView.setViewName("forward:/goods");
//        return modelAndView;
//    }

    @GetMapping("/orders/{id}")
    public String getOrdersById(@PathVariable("id") int id,Model model){
        Orders orders = ordersService.findById(id);
        model.addAttribute("orders",orders);
        return "ordersEdit";
    }


    @PostMapping("/editorders")
    public String addGoods(Orders orders,Model model){

        int u = ordersService.update(orders);
        model.addAttribute("msg","商品信息更新成功！");
        return "forward:/admin/orders";
    }

    @GetMapping("/gotoAddOrders")
    public String gotoAddorders(){  return "ordersAdd";

    }

    @PostMapping("/addOrders")
    public String addOrders(Orders orders){
        int y = ordersService.insert(orders);
        return "forward:/admin/goods";
    }


}
